import 'dart:convert';

import '../../entity/app_version.dart';
import '../../entity/artwork_model.dart';
import '../../entity/palette_model.dart';

class GlobalConfigLoader {
  /// 加载画布配置
  static CanvasConfig loadCanvasConfig(String json) {
    if (json.isEmpty) {
      return CanvasConfig.fromJson({});
    }
    Map<String, dynamic> jsonMap = jsonDecode(json);
    var ver = AppVersion.fromVersionString(jsonMap['version']);
    if (ver == null) {
      return CanvasConfig.fromJson({});
    }
    return CanvasConfig.fromJson(jsonMap['value']);
  }

  /// 加载调色板配置
  static List<PaletteModel> loadPaletteSetting(String json) {
    if (json.isEmpty) {
      return [];
    }
    Map<String, dynamic> jsonMap = jsonDecode(json);
    var ver = AppVersion.fromVersionString(jsonMap['version']);
    if (ver == null) {
      return [];
    }
    return (jsonMap['value'] as List<dynamic>)
        .map((e) => PaletteModel.fromJson(e))
        .toList();
  }
}
